<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="腹透号" prop="patientIdcard">
        <el-input
          v-model="queryParams.patientIdcard"
          placeholder="请输入腹透号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="patientPhone">
        <el-input
          v-model="queryParams.patientPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家庭住址" prop="patientAddress">
        <el-input
          v-model="queryParams.patientAddress"
          placeholder="请输入家庭住址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作单位" prop="patientWork">
        <el-input
          v-model="queryParams.patientWork"
          placeholder="请输入工作单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="微信UID" prop="uidVx">-->
<!--        <el-input-->
<!--          v-model="queryParams.uidVx"-->
<!--          placeholder="请输入微信UID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:info:remove']"
        >删除</el-button>
      </el-col>

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['business:info:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="用户编号" align="center" prop="patientId" />-->
      <el-table-column label="姓名" align="center" prop="patientName" />
      <el-table-column label="腹透号" align="center" prop="patientIdcard" />
      <el-table-column label="联系电话" align="center" prop="patientPhone" />
      <el-table-column label="家庭住址" align="center" prop="patientAddress" />
      <el-table-column label="工作单位" align="center" prop="patientWork" />
<!--      <el-table-column label="状态" align="center" prop="status" />-->
<!--      <el-table-column label="微信UID" align="center" prop="uidVx" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:info:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleAddPlan(scope.row)"
          >每日计划</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改患者对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="腹透号" prop="patientIdcard">
          <el-input v-model="form.patientIdcard" placeholder="请输入腹透号" />
        </el-form-item>
        <el-form-item label="联系电话" prop="patientPhone">
          <el-input v-model="form.patientPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="patientAddress">
          <el-input v-model="form.patientAddress" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="工作单位" prop="patientWork">
          <el-input v-model="form.patientWork" placeholder="请输入工作单位" />
        </el-form-item>
<!--        <el-form-item label="微信UID" prop="uidVx">-->
<!--          <el-input v-model="form.uidVx" placeholder="请输入微信UID" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="planTitle" :visible.sync="planOpen" width="500px" append-to-body>
      <el-form ref="planForm" :model="planForm" :rules="planRules" label-width="80px">
        <el-form-item label="频次" prop="changeTimes">
          <el-input v-model="planForm.changeTimes" placeholder="请输入频次" />
        </el-form-item>
        <el-form-item label="首次换液时间" prop="firstChangeTime">
          <el-date-picker clearable
                          v-model="planForm.firstChangeTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择首次换液时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="间隔时间" prop="changeInterval">
          <el-input v-model="planForm.changeInterval" placeholder="请输入间隔时间" />
        </el-form-item>
        <el-form-item label="腹透液浓度" prop="dialysisFluid">
          <el-input v-model="planForm.dialysisFluid" placeholder="请输入腹透液浓度" />
        </el-form-item>
        <el-form-item label="灌入量" prop="injectionQuantity">
          <el-input v-model="planForm.injectionQuantity" placeholder="请输入灌入量" />
        </el-form-item>
        <el-form-item label="干体重" prop="gWeight">
          <el-input v-model="planForm.gWeight" placeholder="请输入干体重" />
        </el-form-item>
        <el-form-item label="标准血压" prop="bp">
          <el-input v-model="planForm.bp" placeholder="请输入标准血压" />
        </el-form-item>
        <el-form-item label="超滤临界值" prop="clljz">
          <el-input v-model="planForm.clljz" placeholder="请输入超滤临界值" />
        </el-form-item>
        <el-form-item label="体重报警百分比" prop="tzbjpct">
          <el-input v-model="planForm.tzbjpct" placeholder="请输入体重报警百分比" />
        </el-form-item>
        <el-form-item label="标准血压百分比" prop="bppct">
          <el-input v-model="planForm.bppct" placeholder="请输入标准血压百分比" />
        </el-form-item>
        <el-form-item label="超滤临界值百分比" prop="clljzpct">
          <el-input v-model="planForm.clljzpct" placeholder="请输入超滤临界值百分比" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addPlan">确 定</el-button>
        <el-button @click="planCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/business/info";
import { getPlanByPatientId, addPlan } from "@/api/business/plan";
export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      planTitle:"",
      // 是否显示弹出层
      open: false,
      planOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientName: null,
        patientIdcard: null,
        patientPhone: null,
        patientAddress: null,
        patientWork: null,
        status: null,
        uidVx: null
      },
      // 表单参数
      form: {},
      planForm:{},
      // 表单校验
      rules: {
      },
      planRules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询患者列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    planCancel() {
      this.planOpen = false;
      this.planReset();
    },
    // 表单重置
    reset() {
      this.form = {
        patientId: null,
        patientName: null,
        patientIdcard: null,
        patientPhone: null,
        patientAddress: null,
        patientWork: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        uidVx: null
      };
      this.resetForm("form");
    },


    // 表单重置
    planReset() {
      this.planForm = {
        patientId: null,
        clljzpct: null,
        bppct: null,
        tzbjpct: null,
        clljz: null,
        bp: null,
        gWeight: null,
        injectionQuantity: null,
        dialysisFluid: null,
        changeInterval: null,
        firstChangeTime: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("planForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.patientId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加患者";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patientId = row.patientId || this.ids
      getInfo(patientId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改患者";
      });
    },
    /** 修改按钮操作 */
    handleAddPlan(row) {
      this.reset();
      const id = row.patientId || this.ids
      getPlanByPatientId(id).then(response => {
        console.info(response.data);
        this.planForm = response.data;
          this.planOpen = true;
        this.planTitle = "新增每日计划";
      });
    },

    /** 提交按钮 */
    addPlan() {
      this.$refs["planForm"].validate(valid => {
        if (valid) {
            addPlan(this.planForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.planOpen = false;
              this.getList();
            });
        }
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.patientId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const patientIds = row.patientId || this.ids;
      this.$modal.confirm('是否确认删除患者编号为"' + patientIds + '"的数据项？').then(function() {
        return delInfo(patientIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
